<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Db;
class Customer extends Controller{
    public function Add(){
        if (session("login")!=1) return $this->error("请先登录");
        if (isset($_POST["userReg"])) { //注册表单提交
            //先处理照片
            $file = request()->file('photo');
            if($file){
                $info = $file->rule("md5")->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    //windows
                    $photoStr="/uploads/".str_replace('\\','/',$info->getSaveName());
                    //linux
                    //$photoStr="/uploads/".$info->getSaveName();    
                }else{
                    $photoStr="";
                    return "图片上传失败！";
                }
            }
            //再处理性别
            if ($_POST["gender"]=="female") {
                $gender=0;
            } else {
                $gender=1;
            }
            $user=[
                "name"=>$_POST["name"],
                "gender"=>$gender,
                "tel"=>$_POST["tel"],
                "address"=>$_POST["address"],
                "firstdate"=>$_POST["firstdate"],
                "photo"=>$photoStr,
                "integral"=>$_POST["points"],
                "clinic"=>session("cid"),
                "doctor"=>$_POST["doctor"]
            ];
            $result=db('customer')->insertGetId($user);
            if($result){    //用户添加成功的前提下
                if($_POST["needcase"]=="true"){ //同时添加病例处理
                    $case=[
                        "uid"=>$result,
                        "yid"=>$_POST["doctor"],
                        "date"=>$_POST["firstdate"],
                        "content"=>$_POST["casecontent"],
                        "ask"=>$_POST["ask"],
                        "prescription"=>$_POST["prescription"],
                        "clinic"=>session("cid")
                    ];
                    $case_result=db("case")->insert($case);
                    if($_POST["needrevisit"]=="true"){ //需要复诊处理
                        db('customer')->where("id",$result)->setField("lastdate",$_POST["revisitdate"]);
                    }
                    if ($case_result) {
                        return $this->success("用户注册成功","/Customer/List");
                    }else{
                        return $this->error("用户注册失败");
                    }
                }else{
                    return $this->success("用户注册成功","/Customer/List");
                }
            }else {
                return $this->error("用户注册失败");
            }

        }else {
            if (session("type")=="user") {
                return \view("user_add");
            }else {
                return $this->error("暂不支持管理员账户直接添加会员，请使用用户账户执行此操作。");
                //return \view("admin_add");
            }
        }
    }

    public function CaseImgUpload(){
        if (session("login")!=1) return $this->error("请先登录");
        $file = request()->file('caseImg');
    
        if($file){
            $info = $file->rule("md5")->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                echo "{\"errno\":0,\"data\":[\"/uploads/";
                //linux:  echo $info->getSaveName();    
                echo str_replace('\\','/',$info->getSaveName());
                echo "\"]}";
            }else{
                // 上传失败获取错误信息
                echo "{\"errno\":1,\"data\":\"上传失败\"";
            }
        }
    }

    public function List(){
        if (session("login")!=1) return $this->error("请先登录");
        if (session("type")=="user") {
            $uid=session("uid");
            $cid=session("cid");
            if(isset($_POST["fitter"])){
                if(!empty($_POST["name"])) 
                    $result=db("customer")->where("doctor",$uid)->where("clinic",$cid)->where("name",$_POST["name"])->paginate(10);
                else if(!empty($_POST["date"])) 
                    $result=db("customer")->where("doctor",$uid)->where("clinic",$cid)->where("firstdate",$_POST["date"])->paginate(10);
                else if(!empty($_POST["phone"])) 
                    $result=db("customer")->where("doctor",$uid)->where("clinic",$cid)->where("tel",$_POST["phone"])->paginate(10);
                else $result=db("customer")->where("doctor",$uid)->where("clinic",$cid)->paginate(10);
            }else{
                $result=db("customer")->where("doctor",$uid)->where("clinic",$cid)->paginate(10);
            }
        }else {
            if (session("cid")==0){ //总管理员               
                if(isset($_POST["fitter"])){
                    $uid=$_POST["doctor"];
                    $cid=$_POST["branch"];
                    if(!empty($_POST["name"])) 
                        $result=db("customer")->where("doctor",$uid)->where("clinic",$cid)->where("name",$_POST["name"])->paginate(10);
                    else if(!empty($_POST["date"])) 
                        $result=db("customer")->where("doctor",$uid)->where("clinic",$cid)->where("firstdate",$_POST["date"])->paginate(10);
                    else if(!empty($_POST["phone"])) 
                        $result=db("customer")->where("doctor",$uid)->where("clinic",$cid)->where("tel",$_POST["phone"])->paginate(10);
                    else $result=db("customer")->where("doctor",$uid)->where("clinic",$cid)->paginate(10);
                }else{
                    $result=db("customer")->paginate(10);
                }
            }else { //店铺管理员
                $cid=session("cid");
                if(isset($_POST["fitter"])){
                    $uid=$_POST["doctor"];
                    if(!empty($_POST["name"])) 
                        $result=db("customer")->where("doctor",$uid)->where("clinic",$cid)->where("name",$_POST["name"])->paginate(10);
                    else if(!empty($_POST["date"])) 
                        $result=db("customer")->where("doctor",$uid)->where("clinic",$cid)->where("firstdate",$_POST["date"])->paginate(10);
                    else if(!empty($_POST["phone"])) 
                        $result=db("customer")->where("doctor",$uid)->where("clinic",$cid)->where("tel",$_POST["phone"])->paginate(10);
                    else $result=db("customer")->where("doctor",$uid)->where("clinic",$cid)->paginate(10);
                }else{
                    $result=db("customer")->where("clinic",$cid)->paginate(10);
                }
            }
        }
        
        $this->assign("arr",$result);
        return $this->fetch('list');
    }

    public function Detail(){
        if (session("login")!=1) return $this->error("请先登录");
        $customer=db("customer")->where("id",\input("id"))->find();
        $doctor=db("doctor")->where("id",$customer["doctor"])->find();
        $case=db("case")->alias('c')->join("doctor d","c.yid=d.id")->where("uid",$customer["id"])->select();
        $this->assign("u",$customer);
        $this->assign("d",$doctor);
        $this->assign("c",$case);
        return $this->fetch('detail');
    }

    public function Edit(){
        if (session("login")!=1) return $this->error("请先登录");
        if(isset($_POST["edit"])){
            if ($_POST["gender"]=="female") {
                $gender=0;
            } else {
                $gender=1;
            }
            $update=[
                "name"=>$_POST["name"],
                "doctor"=>$_POST["doctor"],
                "gender"=>$gender,
                "tel"=>$_POST["tel"],
                "firstdate"=>$_POST["firstdate"],
                "lastdate"=>$_POST["lastdate"],
                "address"=>$_POST["address"],
                "integral"=>$_POST["points"]
            ];
            $res=db("customer")->where("id",input("id"))->where("clinic",session("cid"))->update($update);
            if($res) return $this->success("修改成功");
            else return $this->error("修改失败");
        }
        $user=db("customer")->where("id",input("id"))->find();
        if(\session("cid")!="0") $doctor=db("doctor")->where("clinic",\session("cid"))->select();
        else $this->error("总店管理员暂不支持直接修改用户信息,请使用分店管理员账户修改");
        $this->assign("u",$user);
        $this->assign("d",$doctor);
        return \view("edit");
    }

    public function Del(){
        if (session("login")!=1) return $this->error("请先登录");
        return $this->error("普通用户无权执行此操作，请联系管理员删除");
    }

    public function getYidByCid(){
        return json(db("doctor")->field("id,name")->where("clinic",input("cid"))->select());
    }
    public function getAllCid(){
        return json(db("clinic")->field("id,name")->where("id","NEQ",0)->select());
    }
}